package hn.cch.advanced.thread;


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 线程不安全的类和对象测试用例
 */
public class ThreadUnsafeDemo {

    public static Date date = new Date();

    public static void updateCalendarDate() {
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DATE, 1);
                date = calendar.getTime();
            }
        });
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DATE, 1);
                date = calendar.getTime();
            }
        });
        thread1.start();
        thread2.start();

        System.out.println("date:" + date.toString());
        date = new Date();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            updateCalendarDate();
        }

    }

}
